package java_thinking.unit_12;

/**
 * @Description : 异常丢失案例
 * @author: cww
 * @DateTime: 2019-03-23 20:54
 */

class VeryImportantException extends Exception {

    @Override
    public String toString() {
        return "A very important exception";
    }
}

class HoHumException extends Exception {
    @Override
    public String toString() {
        return "A trivial exception";
    }
}
/**
 * @author asd730520@live.com
 */
public class LostMessage {
    void f() throws VeryImportantException {
        throw new VeryImportantException();
    }
    void dispose() throws HoHumException {
        throw new HoHumException();
    }

    public static void main(String[] args) {
        try {
            LostMessage lm = new LostMessage();
            try {
                //这里的异常会被finally的HoHumException()取代，try里抛出了异常但是还没
                //来得及捕获就被finally中的异常取代了，最后被最外层的try-catch
                lm.f();
            } finally {
                lm.dispose();
            }
        } catch (Exception e) {
            System.out.println(e);
//            e.printStackTrace();
        }
    }
}

/**
 * 这种方法就不会丢失
 */
class LostMessage1 {
    public static void main(String[] args) {
        try {
            LostMessage lostMessage = new LostMessage();
            try {
                lostMessage.f();
            } catch (VeryImportantException e) {
                System.out.println(e);
            } finally {
                lostMessage.dispose();
            }
        } catch (HoHumException e) {
            System.out.println(e);
        }
    }
}

/**
 * 这种方法也不会丢失
 */
class LostMessage3 {
    public static void main(String[] args) {
        try {
            LostMessage lm = new LostMessage();
            try {
                lm.f();
            } finally {
                try {
                    lm.dispose();
                } catch (HoHumException e) {
                    System.out.println(e);
                }
            }
        } catch (VeryImportantException e) {
            System.out.println(e);
        }
    }
}

/**
 * 更简单的一种丢失异常的例子，直接在catch中返回了
 */
class LostMessage2 {
    public static void main(String[] args) {
        try {
            throw new RuntimeException();
        } catch (RuntimeException e) {
            return;
        }
    }
}
